// pages/signIn/signIn.js
const service = require('./signInService.js');
// const productService = require('../../../pages/home/products/productService.js');
const utilService = require('../../../utils/utilService.js');
var app = getApp();
let activityRule = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationJar:{},
    complete: false,
    flag: "已领取" ,
    stepsData:[],
    dataSource: "",
    sweetExplain:"",
    shareMsg: '我发现了一个闲置物品的好平台【趣闲】，特意分享给你！',
    isLoading: true,
  },
  goHome: function (e) {
    wx.navigateBack()
  },
  goDetails: function (e) {
    wx.navigateTo({
      url: '/pages/awardDetails/awardDetails',
    })
  },
  switchChange: function (e) {
    console.log('switch 发生 change 事件，携带值为', e.detail.value)
  },
  _requestDetail() {
    service.requestDetail((date) => {
      this.activityRule = date.activityRule;
      let steps = [];
      for (var i = 0; i < date.tSignInfoEntitys.length; i++) {
        let step = {
          progress: (date.continuousSign - 1) < i ? 0 : 1,
          type: date.tSignInfoEntitys[i].maxCount == date.tSignInfoEntitys[i].minCount ? 1 : 3,
          award: date.tSignInfoEntitys[i].maxCount == date.tSignInfoEntitys[i].minCount ? "+" + date.tSignInfoEntitys[i].maxCount + "个" : "最高" + date.tSignInfoEntitys[i].maxCount,
          day: date.flag ? (date.continuousSign == date.tSignInfoEntitys[i].day ? "今天" : date.tSignInfoEntitys[i].day + "天") : (date.continuousSign + 1 == date.tSignInfoEntitys[i].day ? "今天" : date.tSignInfoEntitys[i].day + "天")
        };
        steps.push(step);
      }

      this.setData({
        sweetExplain: date.sweetExplain,
        stepsProgress: ((date.continuousSign - 1) / date.tSignInfoEntitys.length)*120,
        stepsData: steps,
        flag: date.flag ? '已领取' : '领果冻',
        taskText1: date.taskText1,
        taskText2: date.taskText2,
        isLoading:false
      })
    });
  },
  signleBut:function(){
    console.log(this.flag);
    if (this.data.flag=="已领取"){
      
      return
    }
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
    })

    this.animationJar = animation;

    this.animationJar.rotate(15).step();
    this.animationJar.rotate(-15).step();
    this.animationJar.rotate(15).step();
    this.animationJar.rotate(-15).step();
    this.animationJar.rotate(0).step();
    this.setData({
      animationJar: this.animationJar.export()
    })
    
    service.signIn((date) => {
      var app = getApp();
      app.globalData.userSweets = date.count;
      wx.showToast({
        title: '签到成功',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        flag:'已领取',
        animationJar: this.animationJar.export()
      })
      this._requestDetail();
     }
    )
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //授权
    app.loginPromise.then(() => {
      that._requestDetail();
      let param = new Object();
      param['startPage'] = 0;
      param['type'] = 0;
      that.getShareMessage(2)
      utilService.requestProducts(param, function (data) {
        console.log("product data", data)
        that.setData({
          dataSource: data.items,
          isLoading:false
        })
      });
    });

  },

  /**
    * 提交formid
    */
  submitForm: function (e) {
    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId, 19);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
//获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (res) {
    var title = this.data.shareMsg
    var path = "pages/home/home?userId=" + getApp().globalData.userId
    return {
      title: title,
      path: path,

      success: function (res) {
      },
      fail: function (res) {
      }
    }
  },

  /**
   * 规则说明
   */
  rude: function (e) {
    wx.navigateTo({
      url: '/redPacket/pages/rankings/rankingRule/rankingRule?type=14'
     
    })
  }

})